package com.gillion.grule.client.sync;

import com.gillion.grule.client.core.DroolsRuleCompiler;
import com.gillion.grule.client.exception.ExecutorException;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import org.drools.compiler.builder.impl.KnowledgeBuilderImpl;
import org.drools.core.RuleBaseConfiguration;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.io.Resource;
import org.kie.api.io.ResourceType;
import org.kie.internal.KnowledgeBase;
import org.kie.internal.builder.KnowledgeBuilderConfiguration;
import org.kie.internal.builder.KnowledgeBuilderError;
import org.kie.internal.builder.KnowledgeBuilderFactory;

public class DroolsRuleAssembly implements Serializable {
    private KnowledgeBase knowledgeBase;
    private Map<String, DroolsRuleAssembly> assemblys;
    private String content;
    private String name;
    public String toString() {
        return "DroolsRuleAssembly(knowledgeBase=" + getKnowledgeBase() + ", assemblys=" + getAssemblys() + ", content=" + getContent() + ", name=" + getName() + ")";
    }

    public KnowledgeBase getKnowledgeBase() {
        return this.knowledgeBase;
    }

    public void setKnowledgeBase(KnowledgeBase knowledgeBase) {
        this.knowledgeBase = knowledgeBase;
    }

    public Map<String, DroolsRuleAssembly> getAssemblys() {
        return this.assemblys;
    }

    public void setAssemblys(Map<String, DroolsRuleAssembly> assemblys) {
        this.assemblys = assemblys;
    }

    public String getContent() {
        return this.content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void compile(DroolsRuleCompiler compiler) {
        KnowledgeBuilderConfiguration kbConfig = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
        kbConfig.setProperty("drools.dialect.mvel.strict", "false");
        kbConfig.setProperty("drools.dialect.java.strict", "false");
        KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl)KnowledgeBuilderFactory.newKnowledgeBuilder(kbConfig);
        Resource resource = compiler.compile(this.content);
        knowledgeBuilder.add(resource, ResourceType.DRL);
        RuleBaseConfiguration ruleBaseConfiguration = new RuleBaseConfiguration();
        ruleBaseConfiguration.setJittingThreshold(2147483647);
        if (knowledgeBuilder.getErrors().size() != 0) {
            throw new ExecutorException(1001, ((KnowledgeBuilderError)knowledgeBuilder.getErrors().get(0)).toString());
        }
        this.knowledgeBase = knowledgeBuilder.newKnowledgeBase((KieBaseConfiguration)ruleBaseConfiguration);

        if (this.assemblys != null && !this.assemblys.isEmpty()) {
            Set<String> keySet = this.assemblys.keySet();
            for (String key : keySet)
                ((DroolsRuleAssembly)this.assemblys.get(key)).compile(compiler);
        }
    }
}